package com.baibei.pay.recharge;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.baibei.model.PayModeInfo;
import com.baibei.module.GlideApp;
import com.baibei.module.OnItemSelectListener;
import com.baibei.pay.R;
import com.baibei.pay.R2;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by shenminjie on 2017/10/30.
 * mail:shenminjie@100bei.com
 */

class PayModeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<PayModeInfo> mInfos;
    private OnItemSelectListener<PayModeInfo> mListener;

    List<PayModeInfo> getInfos() {
        return mInfos;
    }

    void setInfos(List<PayModeInfo> mInfos) {
        this.mInfos = mInfos;
    }

    void setListener(OnItemSelectListener<PayModeInfo> mListener) {
        this.mListener = mListener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_pay_way, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof ViewHolder) {
            ViewHolder viewHolder = (ViewHolder) holder;
            final PayModeInfo info = mInfos.get(position);
            GlideApp.with(viewHolder.ivPay).load(info.getPayLogoUrl()).placeholder(R.color.dividerColor).into(viewHolder.ivPay);
            viewHolder.tvPayName.setText(info.getPayName());
            viewHolder.tvPayName.setCompoundDrawablesWithIntrinsicBounds(0, 0, info.isSelect() ? R.drawable.ic_checked : R.drawable.ic_uncheck, 0);
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener != null) {
                        mListener.onItemSelectListener(v, info, position);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mInfos == null ? 0 : mInfos.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R2.id.iv_pay)
        ImageView ivPay;
        @BindView(R2.id.tv_pay_name)
        TextView tvPayName;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
